package es.deusto.herotactics.ingameandroid;

import java.util.ArrayList;
import java.util.Iterator;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import es.deusto.herotactics.ingameandroid.botones.Boton;

/**
 * Pantalla que saldrá sobre la principal con botones para realizar acciones.
 * Será para las acciones de las unidades y para el menú contextual de la partida
 * 
 * @author Ibai
 *
 */
public class Contexto {
	private boolean activo;
	private int x1;
	private int y1;
	private int x2;
	private int y2;
	private ArrayList<Boton> botones;
	private int padding;
	private int linePadding;
	private int screenPadding;
	private int buttonHeight;
	private int screenWidth;
	private int screenHeight;

	public Contexto(int padding, int linePadding, int screenPadding, int buttonHeight,
			int screenWidth, int screenHeight) {
		activo = false;
		botones = new ArrayList<Boton>();
		this.padding = padding;
		this.linePadding = linePadding;
		this.screenPadding = screenPadding;
		this.buttonHeight = buttonHeight;
		this.x1 = screenPadding;
		this.y1 = screenPadding;
		this.x2 = screenWidth - this.screenPadding;
		this.y2 = screenPadding;
		this.screenWidth = screenWidth;
		this.screenHeight = screenHeight;
		
//		activarAcabarTurno = true;
//		activarCancelar = false;
	}
	
	public boolean getActivo() {
		return activo;
	}
	
	public void setPadding(int padding) {
		this.padding = padding;
		colocarBotones();
	}

	public void setLinePadding(int linePadding) {
		this.linePadding = linePadding;
		if (botones.size() > 1)
			colocarBotones();
	}

	public void setScreenPadding(int screenPadding) {
		this.screenPadding = screenPadding;
		this.x1 = screenPadding;
		this.y1 = screenPadding;
		this.x2 = screenWidth - screenPadding;
		this.y2 = screenPadding;
		colocarBotones();
	}

	public void setButtonHeight(int buttonHeight) {
		this.buttonHeight = buttonHeight;
		colocarBotones();
	}

	public void activar() {
		activo = true;
	}
	
	public void destruir() {
		botones.clear();
		activo = false;
	}
	
	public void addBoton(Boton boton) {
		botones.add(boton);
		colocarBoton(boton, botones.size()-1);
	}
	
//	public ArrayList<Boton> getBotones () {
//		return botones;
//	}
	
	public void draw(Canvas canvas, Paint paint) {
		if (activo) {
			paint.setARGB(150, 0, 0, 0);
			canvas.drawRect(0,0,screenWidth,screenHeight,paint);
			paint.setColor(Color.BLACK);
			canvas.drawRect(x1, y1, this.x2, this.y2, paint);
			paint.setColor(Color.YELLOW);
			canvas.drawRect(x1+1, y1+1, this.x2-1, this.y2-1, paint);
			for (Boton boton : botones) {
				boton.draw(canvas, paint);
			}
		}
	}
	
	public void touch(float x, float y) {
		if (x<this.x1 || x>this.x2 || y<this.y1 || y>this.y2) {
			this.destruir();
		} else {
			Boton boton;
			Iterator<Boton> iBoton = botones.iterator();
			boolean salir = false;
			while(!salir && iBoton.hasNext()) {
				boton = iBoton.next();
				if (x >= boton.getX1() && y >= boton.getY1() && x<=boton.getX2() && y<=boton.getY2()) {
					boton.touch();
					salir = true;
				}
			}
		}
	}
	
	/**
	 * Coloca todos los botones de nuevo en el contexto y redibuja el contexto en si
	 */
	private void colocarBotones () {
		int i = 0;
		for (Boton boton : botones) {
			colocarBoton(boton, ++i);
		}
	}
	
	/**
	 * Coloca un botón nuevo en el contexto y redimensiona el contexto para que el botón esté dentro
	 * 
	 * @param boton Botón a colocar
	 * @param pos Posición de la array donde se encuentra
	 */
	private void colocarBoton (Boton boton, int pos) {
		int x1, y1, x2, y2;
		x1 = this.x1 + padding;
		x2 = this.x2 - padding;
		if (pos == 0) {
			y1 = this.y1 + padding;
		} else {
			y1 = botones.get(pos-1).getY2() + linePadding;
		}
		y2 = y1 + buttonHeight;
		this.y2 = y2 + padding;
		boton.setDims(x1, y1, x2, y2);
		if (this.y2 > screenHeight-screenPadding)
			throw new RuntimeException("Not supported amount of buttons");
	}
}
